import 'package:flutter_axrjc/datasource/network/api/FeedbackApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/AssetBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_axrjc/widget/image_picker_dialog.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'state.dart';

class FeedbackLogic extends GetxController {
  final FeedbackState state = FeedbackState();
  @override
  void onInit() {
    super.onInit();
    refreshImag();
  }

  void refreshImag() {
    state.imgList.clear();
    state.imgList.addAll(state.originImgList);
    state.imgList.add("-1");
  }

  Future<void> addImg() async {
    AssetBean? a = await showSingleMedia();
    if (a == null) {
      return;
    }
    state.originImgList.add(a.path);
    refreshImag();
  }

  Future<void> submit() async {
    if (state.content.value.isEmpty) {
      EasyLoading.showToast("请输入问题/意见描述");
      return;
    }
    try {
      await FeedBackApi.submit(
          feedbackType: state.isFeedbackSuggest.value ? "01" : "02",
          feedbackContent: state.content.value,
          contactWay: state.contactWay.value,
          imgFile: state.originImgList);
      EasyLoading.showToast("提交成功，感谢您的反馈");
      Get.back();
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      EasyLoading.showToast("$e");
    }
  }
}
